package com.example.doc.service;

import com.example.doc.VO.DictVO;
import com.example.doc.mapper.DictMapper;
import com.example.doc.model.Dict;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictService {

    @Resource
    DictMapper dictMapper;

    public Integer saveDict(Dict dict){
        return dictMapper.addDict(dict);
    }

    public Integer updateDict(Dict dict){
        return dictMapper.updateDict(dict);
    }

    public Integer deleteDictById(Long id){
        return dictMapper.deleteDictById(id);
    }

    public List<DictVO> getAllDicts(){
        List<Dict> dicts = dictMapper.getAllDicts();
        List<DictVO> dictVOs = new ArrayList<>();
        for (Dict dict: dicts){
            DictVO dictVO = new DictVO();
            BeanUtils.copyProperties(dict,dictVO);
            String parentName = dictMapper.getNameById(dict.getParentId());
            if (StringUtils.isEmpty(parentName)){
                dictVO.setParentName("无");
            }else {
                dictVO.setParentName(parentName);
            }
            dictVOs.add(dictVO);
        }
        return dictVOs;
    }

    public List<Dict> getParentDict(){
        return dictMapper.getParentDict();
    }

}
